package main.java.com.blog.controller;

import com.alibaba.fastjson.JSON;
import com.blog.entity.Art;
import com.blog.service.ArtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Admin on 2017/6/6.
 */
@Controller
@RequestMapping("/art")
public class ArtController {

    @Autowired
    ArtService artService;


    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String add(Art art) {

        String message = "发布成功";
        if (art.getArtTitle().equals("") || art.getArtContext().equals("")) {
            message = "请输入标题或内容";
        }
        artService.addArt(art);
        return message;
    }


    @ResponseBody
    @RequestMapping(value = "/load", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
    public String load() {
        List<Art> artList = artService.findByAll();

        return JSON.toJSON(artList).toString();
    }

    @ResponseBody
    @RequestMapping(value = "/username", method = RequestMethod.POST, produces = "text/json;charset=UTF-8")
    public String userName(String userName) {

        List<Art> artList = artService.findByArtAccount(userName);

        return JSON.toJSON(artList).toString();
    }

    @ResponseBody
    @RequestMapping(value = "/deleteArt", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String deleteArt(String artId) {
        int id = Integer.parseInt(artId);

        int result = artService.deleteById(id);
        if (result == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }

    }
}